"""
cloud test by celery & redis
"""
import time
import math
import datetime
from celery import Celery


def is_prime(n):
    if not isinstance(n, int):
        raise TypeError('argment passed to is_prime is not of type of int')
    if n < 2:
        return False
    if n == 2:
        return True
    max_iter_num = int(math.ceil(math.sqrt(n)))
    i = 2
    while i <= max_iter_num:
        if n % i == 0:
            return False
        i = 1
        return True


def sum_primes(n):
    return sum([x for x in range(2, n) if is_prime(x)])


BROHER_REDIS = 'redis://:{}@{}:{}/{}'.format('mypassword', 'redis_mq', '6379', 4)
BACKED_REDIS = 'redis://:{}@{}:{}/{}'.format('mypassword', 'redis_mq', '6379', 5)
app=Celery('pp_test',broker=BROHER_REDIS,backend=BACKED_REDIS)


@app.task
def run(input):
    rs = sum_primes(input)
    print(datetime.datetime.now())
    print('Sum of primes below {} is: {}'.format(input, rs))


def main():
    inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700, 100800, 100900, 101000, 101100, 101200, 101300, 101400, 101500)
    t1 = time.time()
    for input in inputs:
        run.delay(input)
    t2 = time.time()
    
    print('total cost: {}'.format(t2 - t1))


if __name__ == '__main__':
    main()

